package com.sundi.ecmarket.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.config.ExcelUtils;
import com.sundi.ecmarket.dto.IssueDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcBrand;
import com.sundi.ecmarket.entity.EcIssue;
import com.sundi.ecmarket.service.IEcIssueService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 常见问题表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@RestController
@Slf4j
@RequestMapping("/ecIssue")
public class EcIssueController {



    @Autowired
    IEcIssueService iEcIssueService;

    /**
     * @deprecated ec商城中商场管理里的常见问题修改列表查询模块
     * @author 许家宝
     * @param issueDto
     * @return
     */
    @RequestMapping("/xujiabao/issueList")
    public ResultEntity list(IssueDto issueDto){

        PageHelper.startPage(issueDto.getPageNo(),issueDto.getPageSize());
        List<EcIssue> list = iEcIssueService.selectIssueList(issueDto);
        PageInfo<EcIssue> pageInfo = new PageInfo(list);
        return ResultEntity.ok(pageInfo);
    }

    /**
     * @deprecated ec商城中商场管理里的常见问题修改逻辑删除模块
     * @author 许家宝
     * @param id
     * @return
     */
        @RequestMapping("/xujiabao/logicdel")
        public ResultEntity logindel(Integer id){

            System.err.println(id);
            iEcIssueService.updateLogicDel(id);
            return ResultEntity.ok();
    }

    /**
     * @deprecated ec商城中商场管理里的常见问题添加模块
     * @author  许家宝
     * @param issueDto
     * @return
     */
    @RequestMapping("/xujiabao/issueAdd")
    public ResultEntity add(@RequestBody IssueDto issueDto){
        System.err.println(issueDto);
        EcIssue ecIssue = new EcIssue();
        BeanUtils.copyProperties(issueDto,ecIssue);
        if(ecIssue.getId()==null){
            ecIssue.setAddTime(new Date());
        }
        boolean insert = iEcIssueService.insert(ecIssue);
        return ResultEntity.ok(insert);
    }


    /**
     * @deprecated ec商城中商场管理里的常见问题修改模块
     * @author  许家宝
     * @param issueDto
     * @return
     */
    @RequestMapping("/xujiabao/issueUpd")
    public ResultEntity upd(@RequestBody IssueDto issueDto){
        System.err.println(issueDto);
        EcIssue ecIssue = new EcIssue();
        BeanUtils.copyProperties(issueDto,ecIssue);
        if(ecIssue.getId()!=null){
            ecIssue.setUpdateTime(new Date());
        }
        boolean b = iEcIssueService.updateById(ecIssue);
        return ResultEntity.ok(b);
    }


    @RequestMapping("/xiayidong")
    @Transactional
    public ResultEntity xia(Integer id){

        EcIssue ecIssue = iEcIssueService.selectById(id);
        Integer id1 = ecIssue.getId();

        EcIssue issue = iEcIssueService.getStuXiaOrder(id);
        Integer id2 = issue.getId();
        issue.setId(id1);
        ecIssue.setId(id2);

        iEcIssueService.updateById(ecIssue);
        iEcIssueService.updateById(issue);

        return ResultEntity.ok();
    }

    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(IssueDto issueDto) throws Exception {

        XSSFWorkbook xSSFWorkbooks = iEcIssueService.downLoadGoodsExcel(issueDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("常见问题表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }


    List<String> excelname = Arrays.asList(".xls",".xlsx");
    @RequestMapping("/upLoadExcel")
    public ResultEntity  upLoadExcel(MultipartFile file) throws Exception {
        if (file == null){
            throw new Exception("文件为空");
        }
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        if (!excelname.contains(fileType)){
            throw new Exception("只能导入excel格式的文件");
        }
        try {
            this.iEcIssueService.uplodExcel(file);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new Exception("导入数据失败");
        }
        return ResultEntity.ok("导入数据成功");
    }

}
